﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using PacManPP.Managers;
using Microsoft.Xna.Framework.Input;

namespace PacManPP.Objects
{

    enum MonsterColor
    {
            RED = 4,
            BLUE = 6,
            PINK = 8,
            YELLOW = 10
    }

   

    class Monster : IObject
    {

        public Boolean killed = true;
        public int timeofMove = 1;
        public DateTime stamp;

        public Monster(MonsterColor color, Vector2 pos)
        {
            stamp = DateTime.Now;
            if (color == MonsterColor.BLUE)
            {
                startFrame = (int)MonsterColor.BLUE;
                maxFrame = 8;
                spirteSheetName = "objects";
                Position = pos;
                direction = (int)Directions.Up;
               
            }
            else if (color == MonsterColor.RED) {
                startFrame = (int)MonsterColor.RED;
                maxFrame = 6;
                spirteSheetName = "objects";
                Position = pos;
                direction = (int)Directions.Down;
            }
            else if (color == MonsterColor.PINK)
            {
                startFrame = (int)MonsterColor.PINK;
                maxFrame = 10;
                spirteSheetName = "objects";
                Position = pos;
                direction = (int)Directions.Left;
            }
            else
            {
                startFrame = (int)MonsterColor.YELLOW;
                maxFrame = 12;
                spirteSheetName = "objects";
                Position = pos;
                direction = (int)Directions.Right;
            }

            poistion.X = poistion.X + GameManager.scale / 2 - 2;
            poistion.Y = poistion.Y + GameManager.scale / 2;
        }

        public void makeMove()
        {
            TimeSpan result = stamp - DateTime.Now;
            if (timeofMove == 0 )
            {
                int value = GameManager.RandomNumber(0, 4);
                direction = value;
                timeofMove++;
                stamp = DateTime.Now;                            
                
            }
            else if (timeofMove == 50)
            {
                
                timeofMove = 0;
            }
            else
            {
                timeofMove = timeofMove+1;
                if (direction == (int)Directions.Up)
                {
                    if (BoardManager.mosterWithWall(new Vector2(poistion.X, poistion.Y - 1)) == true)
                    {
                        timeofMove = 0;
                        return;
                    }
                    poistion.Y--;
                    move = (int)Directions.Up;
                }
                else if (direction == (int)Directions.Down)
                {
                    if (BoardManager.mosterWithWall(new Vector2(poistion.X, poistion.Y + 1)) == true)
                    {
                        timeofMove = 0;
                        return;
                    }
                    poistion.Y++;
                    move = (int)Directions.Down;
                }
                else if (direction == (int)Directions.Left)
                {
                    if (BoardManager.mosterWithWall(new Vector2(poistion.X-1, poistion.Y)) == true)
                    {
                        timeofMove = 0;
                        return;
                    }
                    poistion.X--;
                    move = (int)Directions.Left;
                }
                else if (direction == (int)Directions.Right)
                {
                    if (BoardManager.mosterWithWall(new Vector2(poistion.X+1, poistion.Y)) == true)
                    {
                        timeofMove = 0;
                        return;
                    }
                    poistion.X++;
                    move = (int)Directions.Right;
                }
            }
        }


        public virtual void Update(GameTime gameTime)
        {

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            if (timer > interval)
            {
                currentFrame++;
                timer = 0f;
            }

            if (currentFrame == maxFrame)
            {
                currentFrame = startFrame;
            }

            

            sourceRect = new Rectangle(currentFrame * Object_Size, Object_Size * move, Object_Size, Object_Size);
            origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);

        }
        

        
    }
}
